const { contextBridge,desktopCapturer } = require('electron')
const fs = require('fs').promises;
const path = require('path');

// window.addEventListener('DOMContentLoaded', () => {
//   //todo something
// })

contextBridge.exposeInMainWorld(
  'electron',
  {
    //emmm 就算放这里也会报： Uncaught (in promise) DOMException: Permission denied
    getDisplayMediaAndSet(){

      desktopCapturer.getSources({ types: ['window', 'screen'] }).then(async sources => {
        console.log('sources:',sources);
        for (const source of sources) {
          //todo 可以让用户自己选 要分享整个窗口 还是某个window
          if (source.name === 'Entire Screen') {
            try {
              const stream = await navigator.mediaDevices.getUserMedia({
                audio: false,
                video: {
                  mandatory: {
                    chromeMediaSource: 'desktop',
                    chromeMediaSourceId: source.id,
                    minWidth: 1280,
                    maxWidth: 1280,
                    minHeight: 720,
                    maxHeight: 720
                  }
                }
              })

              // ↓ 这样也不行 会报： VM180 F:\js_workplace\tutorial\electron\electron_tutorial\06.mediaDevices\preload.js:45 DOMException: Permission denied
              /*const stream = await window.navigator.mediaDevices.getDisplayMedia({
                video:{
                  // id: source.id
                  id: source.display_id
                },
                audio:false
              })*/

              player.srcObject = stream;
            } catch (e) {
              console.error(e)
            }
            return
          }
        }
      })

    }


  }
)
